package occ.service
{
  import occ.model.ReceiptVO;
  import occ.model.ReservationType;
  import occ.model.ReservationVO;
  import occ.model.SiteTypeVO;
  
  import mx.utils.*;
  
  import occ.utils.*;
  import com.greatwideweb.util.*;
  
  public class ReceiptBuilder
  {
    private var r:ReservationVO;
    private var _receipt:ReceiptVO;
    private var siteType:SiteTypeVO;
    
    public function ReceiptBuilder(_r:ReservationVO)
    {
      r = _r; 
      siteType = r.siteData.siteType;
       _receipt = new ReceiptVO();
      
       if(r.reservationType == ReservationType.SEASON)
        buildSeason();
      else if(r.reservationType == ReservationType.MONTHLY)
        buildMonthly();
      else
        buildDaily();
    }
    
    public function get receipt():ReceiptVO
    {
      if( _receipt == null)
        throw new Error("ReceiptBuilder improperly implemented");
      return _receipt;
    }
    
    private function buildDaily():void
    {
      if( _receipt == null)
        throw new Error("ReceiptBuilder improperly implemented");
      trace("siteType");
      trace(ObjectUtil.toString(siteType))
      var guestCount:int = (r.guestCount - siteType.dailyDefaultGuestCount) + siteType.dailyDefaultGuestCount;
      var weekCount:int=0;
      var weekendCount:int=0;
      var currentDate:Date;
      var additionalGuests:int=0;
      for(var i:int=0; i<r.duration;i++){
        currentDate = new Date(r.startDate.getFullYear(), r.startDate.getMonth(), r.startDate.getDate() + i);
        if(DateUtils.isWeekendHoliday(currentDate))
          weekendCount++;
        else
          weekCount++;
      }
     
     trace("weekendCount: " + weekendCount);
      trace("weekCount: " + weekCount);
      trace(" r.duration: " +  r.duration);
      if((weekendCount + weekCount)!= r.duration)
        throw new Error("Housten we have a problem with the duration ");
      
     
      _receipt.baseAmount = weekendCount * siteType.dailyRateWeekend;
      _receipt.baseAmount += weekCount * siteType.dailyRateWeek;
      
      trace("before additional Guests: " + _receipt.baseAmount);
      
      if(siteType.dailyDefaultGuestCount > r.guestCount)
        r.guestCount = siteType.dailyDefaultGuestCount;
        
      additionalGuests = r.guestCount - siteType.dailyDefaultGuestCount;
      
      if(additionalGuests > 0)
         _receipt.baseAmount += additionalGuests * siteType.dailyAddGuestRate;
      trace("after additional Guests: " + _receipt.totalAmount);
      _receipt.totalAmount = _receipt.baseAmount;
      if(r.discountAmount > 0){
        _receipt.discount = r.discountAmount;
        _receipt.totalAmount -= _receipt.discount;
      }else
        _receipt.discount = 0;
        
        
      trace("after discount: " + _receipt.totalAmount);

      if(siteType.taxRate > 0){
        _receipt.taxAmount = _receipt.totalAmount * siteType.taxRate ;
        _receipt.totalAmount +=_receipt.taxAmount;
      }else
         _receipt.taxAmount = 0;
      
      trace("after taxes: " + ObjectUtil.toString(_receipt));
    }
    
    private function buildMonthly():void
    {
      trace("buildMonthly called");
      if( _receipt == null)
        throw new Error("ReceiptBuilder improperly implemented");
        
      _receipt.baseAmount = siteType.monthRate;
      
      
      if(r.guestCount > siteType.monthDefaultGuestCount)
      {
        _receipt.baseAmount = (r.guestCount - siteType.monthDefaultGuestCount) * siteType.monthAddGuestRate;
      }
      
      _receipt.totalAmount = _receipt.baseAmount;
      
      if(r.discountAmount > 0)
      {
        _receipt.discount = r.discountAmount;
        _receipt.totalAmount -= _receipt.discount;
      }else
        _receipt.discount = 0;
      
       if(siteType.taxRate > 0)
       {
        _receipt.taxAmount = _receipt.totalAmount * siteType.taxRate;
        _receipt.totalAmount +=_receipt.taxAmount;
      }else
        _receipt.taxAmount = 0;
      
      trace("in monthly after taxes: " + ObjectUtil.toString(_receipt));
    }
    
    private function buildSeason():void
    {
       trace("buildSeason called");
      if( _receipt == null)
        throw new Error("ReceiptBuilder improperly implemented");
        
      _receipt.baseAmount = siteType.seasonRate;
      _receipt.totalAmount = _receipt.baseAmount;
      
      if(r.discountAmount > 0)
      {
        _receipt.discount = r.discountAmount;
        _receipt.totalAmount -= _receipt.discount;
      }else
         _receipt.discount = 0;
      
       if(siteType.taxRate > 0)
       {
        _receipt.taxAmount = _receipt.totalAmount * siteType.taxRate;
        _receipt.totalAmount +=_receipt.taxAmount;
      }else
        _receipt.taxAmount = 0;
    }
  }
}